package com.zrar.ak.gkpt.controller;

import com.zrar.ak.gkpt.jpa.User;
import com.zrar.ak.gkpt.jpa.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户相关接口", description= "提供用户相关的接口文档")
@RequestMapping("/demos/")
public class OpenControlller {
    @Resource
    UserRepository userRepository;

    @Resource
    JdbcTemplate jdbcTemplate;

    Logger logger = LoggerFactory.getLogger(OpenControlller.class);

    @PostMapping(value = "taskstatus", produces = MediaType.TEXT_PLAIN_VALUE)
    public String updateStatus(@RequestParam String id, @RequestParam String status) {
        logger.info("id = {}, status = {}", id, status);
        return "更新成功";
    }

    /**
     * 心跳测试
     * @return 字符串
     */
    @GetMapping(value = "heartbeat", produces = MediaType.TEXT_PLAIN_VALUE)
    public String heartbeat() {
        return "I'm alive plain text!";
    }

    /**
     * 测试方法
     * @param request http请求
     * @return json
     */
    @GetMapping(value ="test",produces = MediaType.APPLICATION_JSON_VALUE)
    public String test(HttpServletRequest request)  {
        String uri = request.getRequestURI();
        return "{\"uri\":\"" + uri +  "\"}";
    }
    @CrossOrigin(origins = "http://localhost:8088")
    @PostMapping(value = "user", produces = MediaType.APPLICATION_JSON_VALUE)
    public User save(@RequestBody User user){
        user.setUpdateTime(LocalDateTime.now());
        user.setNote("test");
        return userRepository.save(user);
    }


    @RequestMapping("/test3")
    @ApiOperation(value = "申请延续申请", httpMethod = "GET", notes = "申请延续申请")
    public List<Map<String, Object>> test3(){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from form_build_unit_t");
        return maps;
    }
}
